<?php
/**
 * @copyright Arthur Beer
 * @package reflection
 *
 * @copyright TinyWEB CMS
 * @link http://tinywebcms.ru
 * @link http://cms.vingrad.ru
 * @version SVN: $Id$ 
 */
 
/**
 * MakeClassFile
 */
class MakeClassFile extends Object
{
    protected $class_name = 'Example';
    protected $class_type = 'class';
    protected $class_extends = 'extends Object';
    protected $class_implements = '';
    protected $header_title = 'Silent Project';
    protected $header_package = 'global';
    protected $header_copyright = 'Arthur Beer (C) %Y';
    
    private $chmodf = 0644;
    private $chmodd = 0755;
    private $path;
    private $file;
    private $dir;
    private $workpath = '../';
    
    public function __construct($args)
    {
        if(count($args) < 1) {
            die('Please write class pathname');
        }

        while($arg = array_shift($args)) {
            switch($arg) {
                case 'extends':
                    $this->class_extends = 'extends ' . $arg = array_shift($args);
                    break;
                case 'implements':
                    $this->class_implements = 'implements ' . $arg = array_shift($args);
                    break;
                case 'abstract':
                case 'interface':
                    $this->class_type = $arg . ($arg == 'abstract' ? ' class' : '');
                    $arg = array_shift($args);
                default:
                    // Define class name.
                    // Set class path as package (Java Style).
                    $def = explode('/', $arg);
                    $this->class_name = array_pop($def);
                    $this->header_package = join('.', $def);
                    
                    $this->path = $this->workpath . '/' . $arg . '.php';
                    $this->file = $this->class_name . '.php';
                    $this->dir = $this->workpath . '/' . join('/', $def);
                    
                    if($this->class_type == 'interface') {
                    	$this->class_extends = null;
					}
            }
        }
        
        $this->header_copyright = str_replace("%Y", date("Y"), $this->header_copyright);
    }
    
    public function exec()
    {
        if(!is_dir($this->dir) && !mkdir($this->dir, $this->chmodd, true)) {
            die('Cannot create class directory.');
        }
        
        if(file_exists($this->path)) {
            die('Class file is exists!');
        }
        
        if(!touch($this->path)) {
            die('Cannot create class file.');
        }
        
        if(chmod($this->path, $this->chmodf)) {
            echo 'Cannot chmod class file.';
        }
        
        file_put_contents($this->path, $this->getTemplate());
        
        return true;
    }
    
    public function getTemplate()
    {
    	$body = '';
    	if($this->class_type != 'interface') {
    		$body = <<<BODY
    /**
     * Конструктор класса
     * 
     */
    public function __construct()
    {
    }
BODY;
		}

        return <<<TEMPLATE
<?php
/**
 * $this->header_title
 *
 * @copyright $this->header_copyright
 * @package $this->header_package
 *
 * @version SVN: \$Id$ 
 */
 
/**
 * $this->class_name
 */
$this->class_type $this->class_name $this->class_extends $this->class_implements
{
$body
}

/**
 * Tab: 4 spaces
 * Encoding: utf-8
 */
?>
TEMPLATE;
    }
}

?>